<?php
class pro_tenant_model  extends MY_Model {
	var $model = 'pro_tenant_model';

	function GetJSON()
	{
		$ls_sql	= "	SELECT	a.no_tenant,a.nama_tenant,a.no_toko,a.luas_tempat,a.lokasi_tempat,a.id_lokasi,
							a.jenis_usaha,a.alamat_lengkap,a.id_sewa,
							a.status_tenant,a.status_anggota,a.no_anggota,
							a.no_id_card,a.no_telephone,a.no_handphone,
							a.no_kontrak,TO_CHAR(a.periode_awal_sewa,'DD/MM/YYYY') periode_awal_sewa,
							TO_CHAR(a.periode_akhir_sewa,'DD/MM/YYYY') periode_akhir_sewa,a.jangka_waktu,
							a.harga_sewa,a.keterangan
					FROM	pro_tenant a
					WHERE	UPPER(a.status_tenant) = 'TENANT' AND
							(UPPER(a.no_tenant) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.nama_tenant) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.no_toko) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.jenis_usaha) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.lokasi_tempat) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.luas_tempat) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.alamat_lengkap) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.status_anggota) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.no_anggota) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.no_id_card) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.no_telephone) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.no_handphone) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.status_tenant) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.no_kontrak) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.periode_awal_sewa) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.periode_akhir_sewa) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.jangka_waktu) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.harga_sewa) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.keterangan) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.id_lokasi' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order')).",REGEXP_REPLACE(a.no_toko, '[0-9]', ''), TO_NUMBER(REGEXP_SUBSTR(a.no_toko, '[0-9]+'))";	
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('pro_tenant', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('pro_tenant', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_no_tenant){
		$result = $this->db->query("	SELECT	a.no_tenant,a.nama_tenant,a.no_toko,a.luas_tempat,
												a.jenis_usaha,a.alamat_lengkap,
												a.status_tenant,a.status_anggota,a.no_anggota,
												a.no_id_card,a.no_telephone,a.no_handphone,
												a.no_kontrak,TO_CHAR(a.periode_awal_sewa,'DD/MM/YYYY') periode_awal_sewa,
												TO_CHAR(a.periode_akhir_sewa,'DD/MM/YYYY') periode_akhir_sewa,a.jangka_waktu,
												a.harga_sewa,a.keterangan,a.lokasi_tempat,a.id_lokasi,a.id_sewa
										FROM	pro_tenant a
										WHERE a.no_tenant = '$as_no_tenant'");
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_no_tenant)
	{
		$result = $this->db->query("	SELECT	a.no_tenant,a.nama_tenant,a.no_toko,a.luas_tempat,
												a.jenis_usaha,a.alamat_lengkap,
												a.status_tenant,a.status_anggota,a.no_anggota,
												a.no_id_card,a.no_telephone,a.no_handphone,
												a.no_kontrak,TO_CHAR(a.periode_awal_sewa,'DD/MM/YYYY') periode_awal_sewa,
												TO_CHAR(a.periode_akhir_sewa,'DD/MM/YYYY') periode_akhir_sewa,a.jangka_waktu,
												a.harga_sewa,a.keterangan,a.lokasi_tempat,a.id_lokasi,a.id_sewa
										FROM	pro_tenant a
										WHERE a.no_tenant = '$as_no_tenant'");
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM pro_tenant WHERE no_tenant= '".$arr_rule['no_tenant']."'"))<=0)
			{
				return $this->error_delete.' [id: '.$arr_rule['no_tenant']."]<br />";
			}else{
				$this->db->delete('pro_tenant', array('no_tenant' => $arr_rule['no_tenant']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>